package  
{
	/**
	 * ...
	 * @author Lisko
	 */
	public class group 
	{
		public var uncalculated_sosedi:Array = [];
		public var stones:Array = [];
		public var dame:Array = [];
		public var color:int;
		public function group(x:int, y:int) 
		{
			get_stones(x, y);
			//trace(stones.length, dame.length);
		}
		
		public function get_stones(x:int, y:int):void
		{
			if (x<1 || y<1 || x>19 || y>19) 
			{
				return;
			}
			if (board.stones[x - 1][y - 1]!=0)
			{
				stones.push([x, y]);
				color = board.stones[x - 1][y - 1];
				//trace("---------------------");
				//trace("new stone ", x," ", y);
				add_sosedi(x,y);
			}
			while (uncalculated_sosedi[0]) 
			{
				var curX:int = uncalculated_sosedi[0][0];
				var curY:int = uncalculated_sosedi[0][1];
				if (board.stones[curX-1][curY-1]==color && !find_in_stones(curX,curY))
				{
					stones.push([curX, curY]);
					//trace("new stone ", curX," ", curY);
					add_sosedi(curX, curY);
				}
				else
				if (board.stones[curX-1][curY-1]==0)
				{
					if (!find_in_dame(curX,curY))
					{
						dame.push([curX, curY]);
						//trace("new dame ", curX," ", curY);
					}
				}
				delete uncalculated_sosedi[0];
				var tmp:Array = [];
				for each (var s:Array in uncalculated_sosedi)
				{
					tmp.push(s);
				}
				uncalculated_sosedi = tmp;
			}
		}
		
		private function add_sosedi(x:int, y:int):void 
		{
			//trace("[add_sosedi]: ", find_in_stones(x - 1, y), find_in_stones(x + 1, y), find_in_stones(x, y - 1), find_in_stones(x, y + 1));
			if (x<1 || y<1 || x>19 || y>19) 
			{
				return;
			}
			if (!find_in_stones(x - 1, y) && x>1)
				uncalculated_sosedi.push([x - 1, y]);
			if (!find_in_stones(x + 1, y) && x<19)
				uncalculated_sosedi.push([x + 1, y]);
			if (!find_in_stones(x, y - 1) && y>1)
				uncalculated_sosedi.push([x, y - 1]);
			if (!find_in_stones(x, y + 1) && y<19)
				uncalculated_sosedi.push([x, y + 1]);
		}
		
		public function find_in_stones(x:int, y:int ):Boolean
		{
			var res:Boolean = false;
			for (var i:int = 0; i < stones.length; i++) 
			{
				if (stones[i][0] == x && stones[i][1] == y)
					res = true;
			}
			return res;
		}
		
		public function find_in_dame(x:int, y:int ):Boolean
		{
			var res:Boolean = false;
			for (var i:int = 0; i < dame.length; i++) 
			{
				if (dame[i][0] == x && dame[i][1] == y)
					res = true;
			}
			return res;
		}
		
		public function eat():int
		{
			for (var i:int = 0; i < stones.length; i++) 
			{
				board.stones[stones[i][0]-1][stones[i][1]-1] = 0;
				var stone_to_delete:stone = board.stones_symbs[stones[i][0]-1][stones[i][1]-1];
				if (stone_to_delete)
				{
					if (board.is_check_ko)
						board.check_ko_deleted_stones.push(stone_to_delete);
					stone_to_delete.parent.removeChild(stone_to_delete);
				}
				board.stones_symbs[stones[i][0]-1][stones[i][1]-1] = null;
			}
			var res:int = stones.length;
			board.last_eated_count = stones.length;
			board.last_eated_x = stones[stones.length - 1][0];
			board.last_eated_y = stones[stones.length - 1][1];
			stones = [];
			dame = [];
			uncalculated_sosedi = [];
			var last_stone_group:group = new group( board.lastX, board.lastY);
			
			if (board.last_eated_count == 1 && last_stone_group.dame.length == 1 && last_stone_group.stones.length == 1)
			{
				Main.brd.set_ko(board.last_eated_x, board.last_eated_y);
			}
			return res;
		}
		
	}

}